﻿/*商品 js */
function WorkSubmitData() { }
WorkSubmitData.Type = "addorder"; //addorder立即购买、addshopcart添加购物车
WorkSubmitData.CommodityId = "";//商品ID值由调用页面传入
WorkSubmitData.WorkId = ''; //作品ID 值由页面传入
WorkSubmitData.ProductId = ''; //产品ID 值由页面传入
WorkSubmitData.Sex = ''; //性别 值由页面传入
WorkSubmitData.Size = ""; //尺码
WorkSubmitData.SizeCode = ""; //尺码编号
WorkSubmitData.CategoryId = 2;//品类 2鞋、8手机壳

(function ($) {
    $.fn.extend({
        addshopcart: function () {//添加到购物车
            var options = {};
            $(this).click(function () {
                //alert("addshopcart");
                WorkSubmitData.Type = "addshopcart";
                jQuery.submit();
            });
        },

        addorder: function () {//直接下单
            var options = {};
            $(this).click(function () {
                WorkSubmitData.Type = "addorder";
               // alert("addorder");
                jQuery.submit();
            });
        },

        add: function () {//增加数量
            var options = {};
            $(this).click(function () {
                var num;
                try {
                    num = parseInt($('[idx="WorkNum"]').val());
                } catch (e) {
                    num = 1;
                }
                num = num + 1;
                if (num > 99)
                    num = 99;
                $('[idx="WorkNum"]').val(num);
            });
        },

        minus: function () {//减少数量
            var options = {};
            $(this).click(function () {
                var num;
                try {
                    num = parseInt($('[idx="WorkNum"]').val());
                } catch (e) {
                    num = 2;
                }
                num = num - 1;
                if (num < 1)
                    num = 1;
                $('[idx="WorkNum"]').val(num);
            });
        }
    });

    $.extend({

        submit: function () {
            //基础数据判断
            var checkR = jQuery.checkForm();
            if (!checkR)
                return false;

            //检测是否登录
            if (!idxbase.CheckLogin()) {
                idxbase.GotoLogin();
                idxbase.Delegate.AddF(jQuery.submit);
            }
            else {
                var _sex = 0;
                if (WorkSubmitData.Sex == "3")
                    _sex = 1;
                else
                    _sex = WorkSubmitData.Sex;
                var qa = $(".detail-choiceItem div span.active");
                for (var i = 0; i < qa.length; i++) {
                    WorkSubmitData.Size = $(qa).eq(i).text();
                    WorkSubmitData.SizeCode = $(qa).eq(i).attr("data-sizecode");
                }
                var submitform = jQuery.createForm({
                    Type: WorkSubmitData.Type,
                    CommodityId: WorkSubmitData.CommodityId,
                    WorkId: WorkSubmitData.WorkId,
                    ProductId: WorkSubmitData.ProductId,
                    Size: WorkSubmitData.Size,
                    SizeCode: WorkSubmitData.SizeCode,
                    WorkNum: $(".detail-countAmount").text(),
                    Sex: _sex,
                    PromotionCode: WorkSubmitData.PromotionCode
                });

                jQuery.submitForm({ form: submitform });
            }
        },

        //setFistImg: function () {//设置第一张图选中
        //    $("#cwGalleryThumb li").first().attr("class", "selected");
        //},

        checkForm: function () {
            if (WorkSubmitData.CategoryId == 2 || WorkSubmitData.CategoryId == 16) {
                if (WorkSubmitData.Size == "") {
                    alert("请选择尺码！");
                    return false;
                }
            }

            return true;
        },

        createForm: function (iptTipO) {//创建表单
            var options = {
                Type: "addshopcart", //addorder:直接下单,addshopcart:添加购物车
                CommodityId: 0,
                WorkId: 0,
                ProductId: 0,
                Sex: "", //性别
                Size: "", //尺码
                SizeCode: "", //尺码编号
                WorkNum: 0,
                PromotionCode:""
            };
            if (iptTipO) { var o = $.extend(options, iptTipO) }
            var form = '<form action="/Commodity/CommoditySubmit" method="post" id="workForm" name="workForm">';
            form += '<input type="text" name="Type" value="' + o.Type + '" />';
            form += '<input type="text" name="CommodityId" value="' + o.CommodityId + '" />';
            form += '<input type="text" name="WorkId" value="' + o.WorkId + '" />';
            form += '<input type="text" name="ProductId" value="' + o.ProductId + '" />';
            form += '<input type="text" name="Sex" value="' + o.Sex + '" />';
            form += '<input type="text" name="Size" value="' + o.Size + '" />';
            form += '<input type="text" name="SizeCode" value="' + o.SizeCode + '" />';
            form += '<input type="text" name="WorkNum" value="' + o.WorkNum + '" />';
            form += '<input type="text" name="CategoryId" value="' + WorkSubmitData.CategoryId + '" />';
            form += '<input type="text" name="PromotionCode" value="' + o.PromotionCode + '" />';
            form += '</form>';
            return form;
        },

        submitForm: function (ipt) {//提交
            var options = {
                form: ""
            };
            if (ipt) { var o = $.extend(options, ipt) }

            $('[idx="form-submit"]').html(o.form);

            jQuery.ajax({
                url: "/Commodity/CommoditySubmit",
                data: $('#workForm').serialize(),
                type: "POST",
                beforeSend: function () {

                },
                error: function (request) {
                    alert(request);
                },
                success: function (data) {
                    var result = data.split("|")[0];
                    var msg = data.split("|")[1];
                    switch (result) {
                        case "1": //添加购物车成功
                            //alert("添加购物车成功");
                            jQuery.gotoShopCart(msg);
                            break;
                        case "2": //添加订单成功
                            //alert("添加订单成功");
                            jQuery.gotoOrderIndex(msg);
                            break;
                        case "nologin":
                            jQuery.submit();
                        case "103":
                            alert("请选择尺码！");
                            break;
                        case "107":
                            alert("很抱歉，此款不允许加入购物车。您购买的此款使用了其他会员未共享的图片，请选择其他款。");
                            break;
                        case "108":
                            alert("很抱歉，此款缺少材质不能下单，请选择其他款。");
                            break;
                        default:
                            alert(data);
                            break;
                    }
                }
            });
        },

        gotoOrderIndex: function (shopcartId) {//去订单页面
            var loadMsg = dialog();
            loadMsg.show();
            $.ajax({
                type: "post",
                url: "/ShopCart/encodeCatrIds",
                data: { "cartIds": (shopcartId + ",") },
                datatype: "json",
                success: function (data) {
                    loadMsg.close();
                    if (data.state == "0") {
                        window.location.href = "/Members/Order/Index?cartids=" + data.data;
                    } else {
                        console.log(data.message);
                    }
                },
                error: function (e) {
                    loadMsg.close();
                    console.log("error");
                }
            });
        },

        gotoShopCart: function (shopcartId) {//去购物车
            var url = '/ShopCart/?newId=' + shopcartId;
            window.setTimeout('window.location.href=' + url, 3000);
            window.location.href = url;
        },
        //选择男性鞋
        selectMale: function () {
            var sizes = new Array();
            $('[idx^="size-"]').each(function () { sizes.push($(this).attr("idx")); });
            if (sizes != undefined && sizes != null && sizes.length > 0) {
                for (var i = 0; i < sizes.length; i++) {
                    if (sizes[i].split("-")[1] >= 22 && sizes[i].split("-")[1] <= 35)
                        $('[idx="' + sizes[i] + '"]').show();
                    else if (sizes[i].split("-")[1] < 39)
                        $('[idx="' + sizes[i] + '"]').hide();
                    else
                        $('[idx="' + sizes[i] + '"]').show();

                    //去除选中的尺码状态
                    $('[idx="' + sizes[i] + '"]').removeClass().attr("class", "skin-prop");
                }

                //去除选中的尺码
                WorkSubmitData.Size = "";
                WorkSubmitData.SizeCode = "";
            }
        },
        //选择女性鞋
        selectFemale: function () {
            var sizes = new Array();
            $('[idx^="size-"]').each(function () { sizes.push($(this).attr("idx")); });
            if (sizes != undefined && sizes != null && sizes.length > 0) {
                for (var i = 0; i < sizes.length; i++) {
                    if (sizes[i].split("-")[1] > 40)
                        $('[idx="' + sizes[i] + '"]').hide();
                    else
                        $('[idx="' + sizes[i] + '"]').show();

                    //去除选中的尺码状态
                    $('[idx="' + sizes[i] + '"]').removeClass().attr("class", "skin-prop");
                }
                //去除选中的尺码
                WorkSubmitData.Size = "";
                WorkSubmitData.SizeCode = "";
            }
        },

        setDefaultSize: function () {
            var sexs = $('[idx^="SexId-"]').toArray();
            if (sexs != undefined && sexs != null && sexs.length > 1) {
                jQuery.selectMale();
            }
        },

        //初始化省的可选项
        initArea: function () {
            var geturl = "/Commodity/AreaProvinceFare";
            $.ajax({
                url: geturl,
                type: "POST",
                cache: true,
                success: function (data) {
                    areaData = data;
                    if (data != "") {
                        //省ID|省名|运费,省ID|省名|运费,......                       
                        var areaArray = areaData.split(',');
                        var result = "";
                        for (var i = 0; i < areaArray.length; i++) {
                            var a = areaArray[i];
                            //console.debug(a);
                            var _array = a.split('|');
                            result += "<li itemid=\"" + _array[0] + "\" itemValue=\"" + _array[2] + "\">" + _array[1] + "</li>";
                        }
                        $('[node-name="selectList"]').html(result);
                    }
                },
                error: function (e) {
                    alert(e);
                }
            });
        }


    });

    //选择性别
    $('li[idx^="SexId"]').click(function () {
        $(this).attr("class", "skin-prop selt").siblings().attr("class", "skin-prop");
        WorkSubmitData.Sex = $(this).attr("idx").split("-")[1];
        if (WorkSubmitData.Sex == 1)
            jQuery.selectMale();
        else if (WorkSubmitData.Sex == 2)
            jQuery.selectFemale();
        else
            jQuery.selectMale();

    });

    //选择尺码
    $('li[idx^="size"]').click(function () {
        $(this).attr("class", "skin-prop selt").siblings().attr("class", "skin-prop");
        WorkSubmitData.Size = $(this).attr("idx").split("-")[1];
        WorkSubmitData.SizeCode = $(this).attr("idx").split("-")[2];
    });

    //加入购物车
    $('[idx="add-shopcart"]').addshopcart();

    //立即购买
    $('[idx="add-order"]').addorder();

    $('[idx="add"]').add();

    $('[idx="minus"]').minus();

    //jQuery.setFistImg();
    jQuery.setDefaultSize();
    jQuery.initArea();
})(jQuery)